package com.higold.cloud.ui.adapter

import android.content.Intent
import android.util.Log
import android.view.View
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.higold.cloud.R
import com.higold.cloud.mvp.model.bean.regionalList.Child
import com.higold.cloud.mvp.model.bean.regionalList.Data
import com.higold.cloud.ui.activity.ManagerDetailActivity
import com.higold.cloud.view.UpdataAPPProgressBar
import java.text.DecimalFormat

/**
 *    author : lxz
 *    date   : 2019/8/10 14:13
 *    desc   :
 */
class RegionalAdapter(data: List<MultiItemEntity>) : BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>(data) {

    init {
        addItemType(TYPE_LEVEL_0, R.layout.item_report)
        addItemType(TYPE_LEVEL_1, R.layout.item_report_expand)
    }


    override fun convert(holder: BaseViewHolder, item: MultiItemEntity) {
        when (holder.itemViewType) {
            TYPE_LEVEL_0 -> {
                when (holder.layoutPosition % 3) {
                    0 -> holder.setImageResource(R.id.iv_head, R.drawable.ic_user)
                    1 -> holder.setImageResource(R.id.iv_head, R.drawable.ic_user)
                }
                val regional = item as Data
                holder.setText(R.id.tv_manager, regional.saleman)
                        .setText(R.id.tv_job, regional.shortName)
                        .setText(R.id.tv_goal, changeFloatToString(regional.saleAmount) + "w / "
                                + changeFloatToString(regional.missionAmount) + "w本月")
                        .setText(R.id.tv_schedule, changeFloatToString((regional.saleAmount / regional.missionAmount) * 100)
                                + "%")
//                        .setImageResource(R.id.iv, if (lv0.isExpanded) R.mipmap.arrow_b else R.mipmap.arrow_r)
                setProgress(holder, regional)

                holder.itemView.setOnClickListener {
                    val intent = Intent(this.mContext, ManagerDetailActivity::class.java)
                    intent.putExtra("Regional_Info", regional)
                    this.mContext.startActivity(intent)
                }
            }
            TYPE_LEVEL_1 -> {
                val child = item as Child
                holder.setText(R.id.tv_expand_manager, child.saleman_name)
                        .setText(R.id.tv_expand_job, child.shortName)
                        .setText(R.id.tv_expand_goal, changeFloatToString(child.saleAmount) + "w / "
                                + changeFloatToString(child.missionAmount) + "w本月")
                        .setText(R.id.tv_expand_schedule, changeFloatToString((child.saleAmount / child.missionAmount) * 100)
                                + "%")
//                        .setImageResource(R.id.iv, if (lv1.isExpanded()) R.mipmap.arrow_b else R.mipmap.arrow_r)

                setProgress(holder, child)

                holder.itemView.setOnClickListener {
                    val pos = holder.adapterPosition
                }
            }
        }
    }

    companion object {
        private val TAG = RegionalAdapter::class.java.simpleName

        val TYPE_LEVEL_0 = 0
        val TYPE_LEVEL_1 = 1
    }

    private fun changeFloatToString(double: Double): String {
        val decimalFormat = DecimalFormat("##.##")
        return decimalFormat.format(double)
    }

    private fun setProgress(holder: BaseViewHolder, data: MultiItemEntity) {

        val progress = (holder.getView<View>(R.id.pg_manager) as UpdataAPPProgressBar)
        progress.progress = 0f

        val rate = if (data.javaClass.name == Child::class.java.name) {
            val child = data as Child
            ((child.saleAmount / child.missionAmount)* 100).toFloat()
        } else {
            val regional = data as Data
            ((regional.saleAmount / regional.missionAmount)* 100).toFloat()
        }

        when (rate) {
            in 0..50 -> progress.setLevel(0)
            in 51..100 -> progress.setLevel(1)
            else -> progress.setLevel(2)
        }

        progress.progress = rate
        progress.duration = 1500
        progress.startAnim()
    }

}